// by Xeno

private ["_dir_to_set","_m_name","_marker","_bagfence","_d_bagfence","_exit_it","_d_bagfences","_d_fence_type"];

if (d_player_busy_building) exitWith{"You are already busy..." call XfGlobalChat};
if ((player call XfGetHeight) > 5) exitWith {"You must be kidding..." call XfGlobalChat};

_d_bagfences = player getVariable "d_bagfences";
diag_log format["fences %1",_d_bagfences];
if (_d_bagfences >= d_bag_fences_per_player) exitWith {"You have already build too many bagfences." call XfGlobalChat};

// If an argument for another type of fence was passed.
_d_fence_type = "ROUND";
If (count _this > 3) then {
	_d_fence_type = _this select 3;
	if (typename _d_fence_type != "STRING") then {
		_d_fence_type = "ROUND";
	};
};
_d_fence_type = switch (_d_fence_type) do {
	case "SHORT": {"Land_BagFenceShort"};
	case "LONG": {"Land_BagFenceLong"};
	case "CUBE": {"Land_HBarrier1"};
	default {"Land_BagFenceRound"};
};

_d_bagfence = player modeltoworld [0,1.5,0];

if (surfaceIsWater [_d_bagfence select 0, _d_bagfence select 1]) exitWith {
	"It is not possible to place a bag fence into water." call XfGlobalChat;
};
d_player_busy_building = true;

_helper1 = "HeliHEmpty" createVehicleLocal [_d_bagfence select 0, (_d_bagfence select 1) + 2, 0];
_helper2 = "HeliHEmpty" createVehicleLocal [_d_bagfence select 0, (_d_bagfence select 1) - 2, 0];
_helper3 = "HeliHEmpty" createVehicleLocal [(_d_bagfence select 0) + 2, _d_bagfence select 1, 0];
_helper4 = "HeliHEmpty" createVehicleLocal [(_d_bagfence select 0) - 2, _d_bagfence select 1, 0];

_exit_it = false;
if ((abs (((getPosASL _helper1) select 2) - ((getPosASL _helper2) select 2)) > 2) || (abs (((getPosASL _helper3) select 2) - ((getPosASL _helper4) select 2)) > 2)) then {
	"Place not valid. Try another location to place your bag fence." call XfGlobalChat;
	_exit_it = true;
};

for "_mt" from 1 to 4 do {call compile format ["deleteVehicle _helper%1;", _mt]};

if (_exit_it) exitWith {d_player_busy_building = false;};

for "_ic" from 1 to bagfence_build_duration do {
	player playMove "AinvPknlMstpSlayWrflDnon_medic";
	sleep 3;
	waitUntil {animationState player != "AinvPknlMstpSlayWrflDnon_medic"};
	if (!(alive player)) exitWith {};
};
d_player_busy_building = false;
if (!(alive player)) exitWith {"You died before your bag fence was ready." call XfGlobalChat};

_dir_to_set = getdir player - 180;


_bagfence = createVehicle [_d_fence_type, [_d_bagfence select 0, _d_bagfence select 1, 0], [], 0, "CAN_COLLIDE"];
_bagfence setdir _dir_to_set;
[_bagfence, 0] call XfSetHeight;
player setVariable ["d_bagfences", (_d_bagfences + 1)];
_bagfence addEventHandler ["killed",{ _d_bagfences =(player getVariable "d_bagfences"); if (_d_bagfences > 0) then {player setVariable ["d_bagfences", _d_bagfences - 1];};}];

player reveal _bagfence;

_bagfence spawn {
	_i_delay = 5;
	while {alive _this} do {
		if ((player distance _this) > 200) then {
			if (alive player Or (_i_delay <= 0)) then {
				diag_log "BagFence deletion";
				deleteVehicle _this;
			} else {
				diag_log "Bagfence too far";
				// The i_delay will make that the bagfences remain up to 25 seconds after the player dies, so that he comes back in unconscious mode and his distance is back to less than 200 meters
				_i_delay = _i_delay - 1;
			};
		};
		sleep 5;
	};
};
